import { Controller } from "../../mvc/core/Controller";
import { IFeedMemoryModule } from "../../../modules/interfaces/IFeedMemoryModule";
import { IView } from "../../mvc/interfaces/IView";
import { FeedMemoryModule } from "../../../modules/FeedMemoryModule";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { EGateCommand } from "../../../Message/Command/EGateCommand";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { DataEvent } from "../events/DataEvent";
import { GameProxy } from "../mvc/GameProxy";
import { ItemData } from "../resource/info/ItemData";
import { ColorInfo } from "../resource/info/ColorInfo";
import { PulseSharedObject } from "../view/player/PulseSharedObject";
import { Alert } from "../../../com/mui/controls/Alert";
import { CheckBoxWin } from "../view/common/alertwins/CheckBoxWin";
import { ColorConfig } from "../resource/ColorConfig";
import { HTMLUtil } from "../../../com/gengine/utils/HTMLUtil";
import { Language } from "../../../extend/language/Language";
import { Log } from "../../../com/gengine/debug/Log";
import { MessageBlock } from "../../../Framework/MQ/MessageBlock";
import { SAutoFeedMemory } from "../../../Message/Public/SAutoFeedMemory";
type int = number;
//class FeedMemoryController
    
    export  class FeedMemoryController extends Controller
    {
       

        public get feedMemoryModule(): IFeedMemoryModule
        {
            if (null == this._feedMemoryModule) 
            {
                this.initView();
            }
            return this._feedMemoryModule;
        }

        public /* override */ get view(): IView
        {
            return this.initView();
        }

        protected /* override */ initView(): IView
        {
            if (null == this._feedMemoryModule) 
            {
                this._feedMemoryModule = new FeedMemoryModule();
            }
            return this._feedMemoryModule;
        }

        protected /* override */ initServer(): void
        {
            super.initServer();
            NetDispatcher.addCmdListener(EGateCommand._ECmdGateAutoFeedMemory, this.onECmdGateAutoFeedMemory);
            Dispatcher.addEventListener(EventName.FeedMemoryModuleShow, this.onFeedMemoryModuleShow);
            Dispatcher.addEventListener(EventName.FeedMemoryCancelItem, this.onFeedMemoryCancelItem);
            Dispatcher.addEventListener(EventName.FeedMemoryGoSave, this.onFeedMemoryGoSave);
            Dispatcher.addEventListener(EventName.FeedMemoryGoFeed, this.onFeedMemoryGoFeed);
            return;
        }

        /* internal  */onFeedMemoryGoFeed(arg1: DataEvent): void
        {
            GameProxy.autoFeedProxy.feedItem();
            return;
        }

        /* internal  */onFeedMemoryGoSave(arg1: DataEvent): void
        {
            var loc1=arg1.data as int;
            GameProxy.autoFeedProxy.saveAutoFeedMemory(cache.feedMemory.clientArr, loc1);
            return;
        }

        /* internal  */onFeedMemoryCancelItem(arg1: DataEvent): void
        {
            var e: DataEvent;
            var code: int;
            var idata: ItemData;
            var isNeedAlert: Boolean;
            var colorInfoII: ColorInfo;
            var colorNameII: string;
            var str: string;
            var onBtnClickHandlerI: Function;

            var loc1;
            code = 0;
            isNeedAlert = false;
            colorInfoII = null;
            colorNameII = null;
            str = null;
            e = arg1;
            code = e.data as int;
            idata = new ItemData(code);
            if (idata && idata.itemInfo) 
            {
                isNeedAlert = !PulseSharedObject.isTodayNotTips("FeedMemoryCancelItem");
                if (isNeedAlert) 
                {
                    onBtnClickHandlerI = (arg1: int, arg2: Boolean): void
                    {
                        if (arg1 == Alert.OK) 
                        {
                            Alert.resetToDefault();
                            cancelMemory(code);
                        }
                        if (arg2) 
                        {
                            PulseSharedObject.save("FeedMemoryCancelItem", true);
                        }
                        return;
                    }
                    Alert.alertWinRenderer = CheckBoxWin;
                    colorInfoII = ColorConfig.instance.getItemColor(idata.itemInfo.item.color);
                    if (colorInfoII) 
                    {
                        colorNameII = colorInfoII.color;
                    }
                    str = HTMLUtil.addColor(idata.itemInfo.item.name, colorNameII);
                    Alert.show(Language.getStringByParam(71359, str), null, Alert.OK | Alert.CANCEL, null, onBtnClickHandlerI);
                }
                else 
                {
                    this.cancelMemory(code);
                }
            }
            return;
        }

        /* internal  */cancelMemory(arg1: int): void
        {
            var loc1=cache.feedMemory.clientArr.indexOf(arg1);
            var loc2=cache.feedMemory.clientNewArr.indexOf(arg1);
            if (loc1 >= 0) 
            {
                cache.feedMemory.clientArr.splice(loc1, 1);
                if (!this.feedMemoryModule.isHide) 
                {
                    if (loc2 >= 0) 
                    {
                        cache.feedMemory.clientNewArr.splice(loc2, 1);
                        this.feedMemoryModule.updateList(cache.feedMemory.clientArr, cache.feedMemory.clientNewArr);
                    }
                    else 
                    {
                        this.feedMemoryModule.updateList(cache.feedMemory.clientArr, []);
                    }
                }
            }
            else 
            {
                Log.system("喂养记忆数据物品总数组不包含新加数组错误。");
            }
            return;
        }

        /* internal  */onECmdGateAutoFeedMemory(arg1: MessageBlock): void
        {
            var mb: MessageBlock;
            var sa: SAutoFeedMemory;
            var isNeedAlert: Boolean;
            var onBtnClickHandler: Function;

            var loc1;
            isNeedAlert = false;
            mb = arg1;
            sa = mb.messageBase as SAutoFeedMemory;
            cache.feedMemory.sever = sa;
            cache.feedMemory.clientArr = sa.memorys.concat();
            cache.feedMemory.clientNewArr = sa.newItemCodes.concat();
            if (sa.reachMax) 
            {
                isNeedAlert = !PulseSharedObject.isTodayNotTips("FeedMemoryReachMax");
                if (isNeedAlert) 
                {
                    onBtnClickHandler = (arg1: int, arg2: Boolean): void
                    {
                        if (arg1 == Alert.OK) 
                        {
                            Alert.resetToDefault();
                            updateModuleView();
                        }
                        if (arg2) 
                        {
                            PulseSharedObject.save("FeedMemoryReachMax", true);
                        }
                        return;
                    }
                    Alert.alertWinRenderer = CheckBoxWin;
                    Alert.show(Language.getString(71360), null, Alert.OK, null, onBtnClickHandler);
                }
                else 
                {
                    this.updateModuleView();
                }
            }
            else 
            {
                this.updateModuleView();
            }
            return;
        }

        /* internal  */updateModuleView(): void
        {
            if (cache.feedMemory.sever.newItemCodes.length > 0) 
            {
                cache.feedMemory.openType = 0;
                this.feedMemoryModule.show();
                this.feedMemoryModule.updateList(cache.feedMemory.sever.memorys, cache.feedMemory.sever.newItemCodes);
                this.feedMemoryModule.updateType(cache.feedMemory.sever.feedType);
            }
            else if (isViewShow) 
            {
                this.feedMemoryModule.updateList(cache.feedMemory.sever.memorys, []);
                this.feedMemoryModule.updateType(cache.feedMemory.sever.feedType);
            }
            return;
        }

        /* internal  */onFeedMemoryModuleShow(arg1: DataEvent): void
        {
            var loc1=arg1.data as int;
            cache.feedMemory.openType = loc1;
            if (this.feedMemoryModule.isHide) 
            {
                this.feedMemoryModule.show();
                if (cache.feedMemory.sever && cache.feedMemory.sever.memorys) 
                {
                    cache.feedMemory.clientArr = cache.feedMemory.sever.memorys.concat();
                    this.feedMemoryModule.updateList(cache.feedMemory.sever.memorys, []);
                    this.feedMemoryModule.updateType(cache.feedMemory.sever.feedType);
                }
                else 
                {
                    this.feedMemoryModule.updateList([], []);
                    this.feedMemoryModule.updateType(0);
                }
            }
            else 
            {
                this.feedMemoryModule.hide();
            }
            return;
        }

        private /* var */_feedMemoryModule: IFeedMemoryModule;
    }
